package com.zhj.rabbitmq.confirms.addConfirmListener;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @program: rabbimmq-demo
 * @description: 发送者 异步监听 方式 事务
 * @author: zhengjibin
 * @create: 2020-11-18 17:46
 * @Version 1.0
 **/
public class AddConfirmListenerSendMessage {

    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        //配置rabbitmq的链接信息
        factory.setHost("122.112.198.94");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        Connection connection = null;// 定义rabbitmq的网络连接 ，一个连接
        Channel channel = null;//定义一个链接中的通道
        try {
            connection = factory.newConnection();//通过工厂获得连接
            channel = connection.createChannel();//通过连接获得通道

            String queueName = "listener_queue";
            String exchangeName = "listener_exchange";
            String routingkey = "listenerRoutingKey";
            channel.queueDeclare(queueName,true,false,false,null);
            channel.exchangeDeclare(exchangeName,"direct",true);
            channel.queueBind(queueName,exchangeName,routingkey);
            //启动发送者确认模式
            channel.confirmSelect();

            /**
             * 注意：如果消息回调没收到是执行了 channel.close();
             */
            channel.addConfirmListener(new ConfirmListener() {
                //消息确认成功的回调方法
                //参数1 消息编号，从1开始递增
                //参数2 是否确认了多条消息
                @Override
                public void handleAck(long l, boolean b) throws IOException {
                    System.out.println("消息编号:"+ l +"---->是否确认多条消息:"+b);
                }
                //消息没有确认成功的回调方法,此方法被调用说明消息发送失败
                @Override
                public void handleNack(long l, boolean b) throws IOException {
                    System.out.println("异常：：：消息编号:"+ l +"---->是否确认多条消息:"+b);

                }
            });

            /**
             * 需要在发送消息前，先启动好监听
             */
            for (int i = 0;i< 2 ;i++){
                String message = "异步确认 发送者确认模式，消息发送测试 第"+i+"条消息";
                channel.basicPublish(exchangeName,routingkey,null,message.getBytes("UTF-8"));
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("消息发送成功:");
        } catch (IOException e) {
            System.out.println("消息发送失败");
            e.printStackTrace();
        } catch (TimeoutException e) {
            System.out.println("消息发送失败");
            e.printStackTrace();
        } finally {
            try {
                if (channel != null){
                    channel.close();
                }
                if (connection != null){
                    connection.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
        }
    }
}
